package joymouse;

import java.awt.Dimension;
import java.awt.Toolkit;
import net.java.games.input.Controller;

/**
 * Controller for the absolute position of the mouse based on the joystick. This
 * input mode is difficult to use and should probably be only used for gaming.
 *
 * @author Ben Wolsieffer
 */
public class AbsoluteMouseController extends MouseController {

    // The previous mouse position
    private int oldX = 0;
    private int oldY = 0;
    // The position of the center of the screen
    private final int centerX;
    private final int centerY;

    /**
     * Assigns the specified {@link Controller} (ie. joystick) to be this device
     * controller's controller.
     *
     * @param cont The controller to use
     */
    public AbsoluteMouseController(Controller cont) {
        super(cont);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        // The width and height of the screen
        int width = screenSize.width;
        int height = screenSize.height;
        centerX = width / 2;
        centerY = height / 2;
        // Debug prints
//        Logger.getLogger().log("Width: " + width);
//        Logger.getLogger().log("Height: " + height);
//        Logger.getLogger().log("X Offset: " + centerX);
//        Logger.getLogger().log("Y Offset: " + centerY);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void run() {
        int newX = (int) ((xAxis.getPollData() + 1) * centerX);
        int newY = (int) ((yAxis.getPollData() + 1) * centerY);
        // If the new position is equal to the old postiton, don't move the 
        // mouse cursor, so it is not impossible to move it with the real mouse
        if (newX != oldX || newY != oldY) {
            robot.mouseMove(newX, newY);
        }
        oldX = newX;
        oldY = newY;
    }
}
